package com.yckj.activechicken.business.store.service.impl;

import com.yckj.activechicken.business.store.mapper.StorehouseEntityMapper;
import com.yckj.activechicken.business.store.mapper.StorehouseInfoMapper;
import com.yckj.activechicken.business.store.mapper.comp.StorehouseMapperComp;
import com.yckj.activechicken.business.store.model.StorehouseEntity;
import com.yckj.activechicken.business.store.model.StorehouseEntityExample;
import com.yckj.activechicken.business.store.model.StorehouseInfo;
import com.yckj.activechicken.business.store.model.vo.StoreHouseVo;
import com.yckj.activechicken.business.store.service.IStoreHouseService;
import com.yckj.activechicken.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Title:com.yckj.activechicken.business.store.service.impl
 * </p>
 * <p/>
 * <p>
 * Description: 描述【仓库管理业务处理实体】
 * </p>
 * <p/>
 * <p>
 * Copyright: Copyright (c) 2019
 * </p>
 * <p/>
 * <p>
 * Company: 英才汇硕信息技术有限公司
 * </p>
 *
 * @author zhoujy
 * @version 1.0
 * @created 2020/6/11 15:51
 */
@Service
public class StoreHouseServiceImpl implements IStoreHouseService {
    @Autowired
    private StorehouseInfoMapper storehouseInfoMapper;

    @Autowired
    private StorehouseEntityMapper storehouseEntityMapper;

    @Autowired
    private StorehouseMapperComp storehouseMapperComp;

    /**
     * 添加实体仓库
     * @param storehouseInfo
     * @param storehouseEntity
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String,String> addStore(StorehouseInfo storehouseInfo, StorehouseEntity storehouseEntity) {
        Map<String,String> outMap = new HashMap<>(2);

        /**
         * 0、重名性校验
         */
        String storeName = storehouseEntity.getStoreName();
        StorehouseEntityExample entityExample = new StorehouseEntityExample();
        entityExample.createCriteria().andStoreNameEqualTo(storeName);
        List<StorehouseEntity> storehouseEntities = storehouseEntityMapper.selectByExample(entityExample);
        if(StringUtils.isNotEmpty(storehouseEntities)){
            outMap.put("result","false");
            outMap.put("message","该仓库名已经存在，请确认！");
            return outMap;
        }

        /**
         * 1、添加仓库主信息
         */
        storehouseInfoMapper.insertSelective(storehouseInfo);

        /**
         * 2、添加实体仓库信息
         */
        storehouseEntity.setStoreId(storehouseInfo.getStoreId());
        storehouseEntityMapper.insertSelective(storehouseEntity);

        outMap.put("result","true");
        return outMap;
    }

    /**
     * 修改仓库信息
     * @param storehouseInfo
     * @param storehouseEntity
     * @return
     */
    @Override
    public Map<String, String> modifyStore(StorehouseInfo storehouseInfo, StorehouseEntity storehouseEntity) {
        Map<String,String> outMap = new HashMap<>(2);

        //1、重名性校验
        StorehouseEntityExample entityExample = new StorehouseEntityExample();
        entityExample.createCriteria().andStoreNameEqualTo(storehouseEntity.getStoreName());
        List<StorehouseEntity> entityList = storehouseEntityMapper.selectByExample(entityExample);
        if(StringUtils.isNotEmpty(entityList)){
            if(!entityList.get(0).getStoreEntityId().equals(storehouseEntity.getStoreEntityId())){
                outMap.put("result","false");
                outMap.put("message","该仓库名已经存在，请确认！");
                return outMap;
            }
        }

        //2、修改主仓库信息
        storehouseInfoMapper.updateByPrimaryKeySelective(storehouseInfo);

        //3、修改仓库信息
        storehouseEntityMapper.updateByPrimaryKeySelective(storehouseEntity);

        outMap.put("result","true");
        return outMap;
    }

    /**
     * 通过id删除仓库信息
     * @param storehouseInfo
     * @return
     */
    @Override
    public int delStore(StorehouseInfo storehouseInfo) {
        return storehouseInfoMapper.updateByPrimaryKeySelective(storehouseInfo);
    }

    /**
     * 多条件分页查询仓库信息
     * @param storeHouseVo
     * @return
     */
    @Override
    public List<StoreHouseVo> listStore(StoreHouseVo storeHouseVo) {
        return storehouseMapperComp.selectStorehouseList(storeHouseVo);
    }
}
